#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;

//class Person
//{
//public:
//	void Print()
//	{
//		cout << "姓名:" << _name << endl;
//		cout << "年龄:" << _age << endl;
//	}
//protected:
//	string _name;
//	int _age;
//};
//
//class Student :public Person
//{
//public:
//	void Set(const char* name, int age)
//	{
//		_name = name;
//		_age = age;
//	}
//private:
//	int _id;
//};
//
//int main()
//{
//	Student s;
//	s.Set("刘昊", 21);
//	s.Print();
//	return 0;
//}


// Student的_num和Person的_num构成隐藏关系，可以看出这样代码虽然能跑，但是非常容易混淆
//class Person
//{
//protected:
//	string _name = "小李子"; // 姓名
//	int _num = 111;// 身份证号
//};
//class Student : public Person
//{
//public:
//	void Print()
//	{
//		cout << "姓名:" << _name << endl;
//		cout << "身份证号:" << Person::_num << endl;
//		cout << "学号:" << _num << endl;
//	}
//protected:
//	int _num = 999; // 学号
//};
//void Test()
//{
//	Student s1;
//	s1.Print();
//};
//
//
//// B中的fun和A中的fun不是构成重载，因为不是在同一作用域
//// B中的fun和A中的fun构成隐藏，成员函数满足函数名相同就构成隐藏。
//class A
//{
//public:
//	void fun()
//	{
//		cout << "func()" << endl;
//	}
//};
//class B : public A
//{
//public:
//	void fun(int i)
//	{
//		cout << "func(int i)->" << i << endl;
//	}
//};
//void Test2()
//{
//	B b;
//	b.fun(10);
//	b.A::fun();
//}
//=======================================================================================================
namespace lh
{
	class Person
	{
	public:
		Person(const char* name = "peter")
			: _name(name)
		{
			cout << "Person()" << endl;
		}

		Person(const Person& p)
			: _name(p._name)
		{
			cout << "Person(const Person& p)" << endl;
		}

		Person& operator=(const Person& p)
		{
			cout << "Person operator=(const Person& p)" << endl;
			if (this != &p)
				_name = p._name;

			return *this;
		}

		~Person()
		{
			cout << "~Person()" << endl;
		}
	protected:
		string _name; // 姓名
	};
	class Student : public Person
	{
	public:
		Student(const char* name, int num)
			: Person(name)//赋值转换
			, _num(num)
		{
			cout << "Student()" << endl;
		}

		Student(const Student& s)
			: Person(s)
			, _num(s._num)
		{
			cout << "Student(const Student& s)" << endl;
		}

		Student& operator = (const Student& s)
		{
			cout << "Student& operator= (const Student& s)" << endl;
			if (this != &s)
			{
				Person::operator =(s);
				_num = s._num;
			}
			return *this;
		}

		~Student()
		{
			//不能显式调用，自动会调用父类的析构
			/*Person::~Person();*/
			cout << "~Student()" << endl;
		}
	protected:
		int _num; //学号
	};

	void Test3()
	{
		Student s1("jack", 18);
		Student s2(s1);
		Student s3("rose", 17);
		s1 = s3;
	}
}

namespace static_lh
{
	class Person
	{
	public:
		Person() { ++_count; }
	protected:
		string _name; // 姓名
	public:
		//静态整个继承体系只有一份
		static int _count; // 统计人的个数。
	};
	int Person::_count = 0;
	class Student : public Person
	{
	protected:
		int _stuNum; // 学号
	};

	void test4()
	{
		Person p;
		Student s;
		cout << ++Person::_count << endl;
		cout << ++Student::_count << endl;
		cout << &Person::_count << endl;
		cout << &Student::_count << endl;
	}
}

namespace virtual_test
{
	class A
	{
	public:
		int _a;
	};
	// class B : public A
	class B : virtual public A
	{
	public:
		int _b;
	};
	// class C : public A
	class C : virtual public A
	{
	public:
		int _c;
	};
	class D : public B, public C
	{
	public:
		int _d;
	};
	void test4()
	{
		D d;
		d.B::_a = 1;
		d.C::_a = 2;
		d._b = 3;
		d._c = 4;
		d._d = 5;
	}
}

int main()
{
	//Test();
	//lh::Test3();
	//static_lh::test4();
	virtual_test::test4();
	return 0;
}